package com.weixing.mall.provider.service.impl;

import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.provider.api.model.vo.MemberAuthVo;
import com.weixing.mall.provider.api.service.MemberServiceRpc;
import com.weixing.mall.provider.enums.MsgFlagEnum;
import com.weixing.oauth2.common.userdetails.IUserAuthService;
import com.weixing.oauth2.common.userdetails.SecurityUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MemberAuthServiceImpi
 * @Description
 * @Author Small
 * @Date 2019/12/25 9:46
 * @Version 1.0
 **/
@Component("memberAuthService")
@Slf4j
public class MemberAuthServiceImpl implements IUserAuthService {
    @Autowired
    private MemberServiceRpc memberServiceRpc;

    @Override
    public SecurityUser loadUserByUsername(String loginName) {
        Result<MemberAuthVo> voResult = memberServiceRpc.loadUserByUsername(loginName);
        return getSecurityUser(voResult);
    }

    @Override
    public SecurityUser loadUserByOpenId(String openId) {
        Result<MemberAuthVo> voResult = memberServiceRpc.loadUserByOpenId(openId);
        return getSecurityUser(voResult);
    }

    @Override
    public SecurityUser loadUserByXcxOpenId(String openId) {
        Result<MemberAuthVo> voResult = memberServiceRpc.loadUserByXcxOpenId(openId);
        return getSecurityUser(voResult);
    }

    private SecurityUser getSecurityUser(Result<MemberAuthVo> voResult) {
        SecurityUser securityUser = new SecurityUser();
        if (!voResult.isSuccess()) {
            securityUser.setMsgFlag(MsgFlagEnum.EXCEPTION.getValue());
        } else {
            MemberAuthVo authVo = voResult.getData();
            if (null != authVo) {
                securityUser.setEnable(IsEnableEnum.ENABLE.getValue().equals(authVo.getIsEnable()) ? true : false);
                securityUser.setMsgFlag(MsgFlagEnum.SUCCESS.getValue());
                securityUser.setLoginName(authVo.getMobile());
                securityUser.setLoginPwd(authVo.getPassword());
                securityUser.setUserRole(GlobalConstant.UserRole.MEMBER);
                securityUser.setSensitiveData(wrapperSenExt(authVo));
                securityUser.setExtData(wrapperExt(authVo));
                securityUser.setOpenId(authVo.getOpenid());
            } else {
                securityUser.setMsgFlag(MsgFlagEnum.FAIL.getValue());
            }
        }
        return securityUser;
    }


    private Map<String, Object> wrapperExt(MemberAuthVo authVo) {
        Map<String, Object> ext = new HashMap<>();
        ext.put("mr", authVo.getIsAgent().intValue() == 0 ? "m" : "a");
        boolean agentRoleEnable = authVo.isAgentRoleEnable();
        Date maturityTime = authVo.getMaturityTime();
        Date now = new Date();
        String apFlag = "no";
        if (agentRoleEnable && now.compareTo(maturityTime) <= 0) {
            apFlag = "yes";
        }
        ext.put("ap", apFlag);
        return ext;
    }


    private MemAuth wrapperSenExt(MemberAuthVo authVo) {
        MemAuth ext = new MemAuth();
        ext.setMid(authVo.getId());
        ext.setMn(authVo.getMemberNo());
        ext.setMm(authVo.getMobile());
        ext.setOpenId(authVo.getOpenid());
        ext.setXcxopenid(authVo.getXcxopenid());
        // 绑定代理信息
        ext.setAid(authVo.getAgentId());
        ext.setAsn(authVo.getAgentSn());
        ext.setAn(authVo.getAgentName());
        // 客服信息
        ext.setKid(authVo.getKefuId());
        ext.setKn(authVo.getKefuName());
        // 代理信息
        ext.setAf(authVo.getIsAgent());
        ext.setAbid(authVo.getAgentRoleBindId());
        ext.setAbn(authVo.getAgentBindName());
        ext.setAbsn(authVo.getAgentBindSn());
        ext.setAbld(authVo.getAgentBindLockDate());
        ext.setAbcl(authVo.getAgentTreePath());
        ext.setAbdri(authVo.getDealerId());
        ext.setAbdr(authVo.getDealer());
        ext.setAbmt(authVo.getMaturityTime());
        ext.setAbads(authVo.getIsAdsAuth());
        ext.setAdsnum(authVo.getAdsLimit());
        return ext;
    }

    @Override
    public SecurityUser loadUserByMobile(String mobile) {
        Result<MemberAuthVo> voResult = memberServiceRpc.loadUserByMobile(mobile);
        return getSecurityUser(voResult);
    }

    @Override
    public Collection<GrantedAuthority> loadAuthority(Long userId) {
        return null;
    }

    @Override
    public UserDetails loadUserByPhoneAndPassword(String phone, String password) {
        return null;
    }

    @Override
    public UserDetails loadUserByPhoneAndSmsCode(String phone, String smsCode) {
        return null;
    }

    @Override
    public void handlerLoginData(HttpServletRequest request, SecurityUser securityUser) {

    }
}
